package com.ljfiction.book.fragment.transactionrecord;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Android on 2017/12/26.
 */

public class TransetionDashangFragment extends BaseFragment{
    private RecyclerView myshupingrecycler;

    private SmartRefreshLayout refreshMyshupingrecycle;
    private BaseQuickAdapter mAdapter;
    private int page = 1;
    private DaShangBean dataBean;
    private List<DaShangBean.DataBean.ListBean> datas = new ArrayList<>();
    private LinearLayout layout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myshuping_recycaler, null);
        myshupingrecycler = view.findViewById(R.id.myshupingrecycler);
        refreshMyshupingrecycle = view.findViewById(R.id.refresh_myshupingrecycle);
        layout=view.findViewById(R.id.zanwushuju);
        intData();
        intView();

        return view;
    }

    private void intView() {
        myshupingrecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myshupingrecycler.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        myshupingrecycler.setAdapter(mAdapter = new BaseQuickAdapter<DaShangBean.DataBean.ListBean, BaseViewHolder>(R.layout.dashang_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, DaShangBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.dashang_num, "打赏了 " + String.valueOf(item.getSpicyiTicketCount()) + "辣椒")
                        .setText(R.id.dashang_time, TimeTool.getTimeAndChange(String.valueOf(item.getGiveDateTime())))
                        .setText(R.id.dashang_chaptername, "《" + item.getBookName() + "》");
            }
        });
        refreshMyshupingrecycle.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                intData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/spicyirewardticketlogByUserId")

                .params("userid", String.valueOf(UserManageBean.userBean.getUserId()))
                .params("startpage", String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshMyshupingrecycle.finishRefresh();
                        refreshMyshupingrecycle.finishLoadMore();
                        Gson g = new Gson();
                        try {
                            dataBean = g.fromJson(s, DaShangBean.class);
                            if (dataBean.getReturnCode() == 200) {
                                datas.addAll(dataBean.getData().getList());
                                myshupingrecycler.setVisibility(View.VISIBLE);
                                layout.setVisibility(View.GONE);
                                mAdapter.notifyDataSetChanged();
                                }else if(dataBean.getReturnCode()==800){
                                    myshupingrecycler.setVisibility(View.GONE);
                                    layout.setVisibility(View.VISIBLE);
                                }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
